package com.ruoyi.web.controller.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.CopyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.product.domain.StoreProductVo;
import com.ruoyi.store.domain.Store;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.storeclassify.domain.Storeclassify;
import com.ruoyi.storeclassify.service.IStoreclassifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.product.domain.StoreProduct;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2021-11-22
 */
@RestController
@RequestMapping("/product/storeProduct")
public class StoreProductController extends BaseController
{
    @Autowired
    private IStoreProductService storeProductService;
    @Autowired
    private IStoreclassifyService classifyService;
    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreProduct storeProduct)
    {
        startPage();
        List<StoreProduct> list = storeProductService.selectStoreProductList(storeProduct);
        if(list.size() == 0){
            return getDataTable(list);
        }
        List<Storeclassify> classifies = classifyService.selectSecond();
        HashMap<Long, String> map = new HashMap<>();
        classifies.forEach(e->{
            map.put(e.getId(),e.getClassifyName());
        });
        list.forEach(e->{
            if(map.containsKey(e.getClassifyId())){
                e.setClassifyName(map.get(e.getClassifyId()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StoreProduct storeProduct)
    {
        List<StoreProduct> list = storeProductService.selectStoreProductList(storeProduct);
        ExcelUtil<StoreProduct> util = new ExcelUtil<StoreProduct>(StoreProduct.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(storeProductService.selectStoreProductById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoreProduct storeProduct)
    {
        return toAjax(storeProductService.insertStoreProduct(storeProduct));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoreProduct storeProduct)
    {
        storeProduct.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(storeProductService.updateStoreProduct(storeProduct));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('product:storeProduct:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(storeProductService.logicDeleteStoreProduct(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
